package com.neuedu.mvc.controller;

import com.alibaba.fastjson.JSON;
import com.neuedu.mvc.entity.User;
import com.neuedu.mvc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目：      spring-framework
 * 类名:       UserController
 * 创建时间：  2024/3/21 10：02
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */


/**
 * @RequestMapping("/user/* /createUser")
 *      /user/aaa/createUser     √
 *      /user/bbbb/createUser    √
 *      /user/createUser   错误
 *
 *
 *  * @RequestMapping("/user/** /createUser")
 *  *      /user/aaa/aaaa/createUser     √
 *  *      /user/bbbb//asdfassafsaf/createUser    √
 *  *      /user/createUser    √
 *
 *
 * @Requestmapping("/user/createUser??“)
 *          /user/createUserababa
 */













@Controller
@RequestMapping("/user")
public class UserController {


    @Autowired
    IUserService userService;



    @GetMapping("/list")
    public List list(HttpServletResponse response) {

        List<User> userList = userService.selectListEntity();
        return userList;
    }


    /**
     * http://127.0.0.1:8080/json/user/getById?id=10
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/getById")
    public User getById(Integer id) {

        User user = userService.selectByIdEntiry(id);
        return user;
    }








    /**
     * http://127.0.0.1:8080/json/user/get/10
     * @param id
     * @return
     */
    @ResponseBody

    @GetMapping("/get/{id}")
    public User get(@PathVariable  Integer id) {

        User user = userService.selectByIdEntiry(id);
        return user;
    }

    //     {successs:true/false}
    @ResponseBody
    @PostMapping("/save")
    public Map save( @RequestBody   User user  ) {

        Map result = new HashMap();

        boolean success = userService.insert(user);

        result.put("success", success);
        return result;
    }


    /**
     * http://127.0.0.1:8080/json/user/list
     * @return
     */
//    @ResponseBody
//    @GetMapping("/list")
//    public String list(HttpServletResponse response){
//        response.setContentType("application/json;charset=utf-8");
//        List<User> users = userService.selectListEntity();
//        String json = JSON.toJSONString(users);
//        return json;
//    }


    /**
     * http://127.0.0.1:8080/json/user/index
     *
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        System.out.println("UserController.index");
        return "forward:/index.jsp";
    }


}
